import 'package:shop/common/AppStorge.dart';
import 'package:shop/common/EventBus.dart';
import 'package:shop/common/shop_material.dart';
import 'package:shop/commonWidget/MyText.dart';
import 'package:shop/commonWidget/myFlow.dart';
import 'package:shop/service/MemberService.dart';
import 'package:shop/utils/MyToast.dart';
import 'package:shop/view/login/LoginIndex.dart';

class LoginList extends StatefulWidget {
  LoginList({Key key}) : super(key: key);
  @override
  _LoginListState createState() => _LoginListState();
}

class _LoginListState extends State<LoginList> {
  Map loginMap;

  @override
  void initState() {
    super.initState();
    loginMap = MyStorge.login;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading:
            IconButton(icon: Icon(Icons.close), onPressed: () => route.pop()),
        elevation: 0.0,
      ),
      body: Container(
        color: Colors.white,
        padding: EdgeInsets.all(40.w),
        child: Stack(
          children: <Widget>[
            ListView(
              children: <Widget>[
                SizedBox(height: 40.h),
                Center(
                    child: Text("欢迎登录赶马商城",
                        style: TextStyle(
                            fontSize: 42.s, fontWeight: FontWeight.bold))),
                SizedBox(height: 80.h),
                MyFlow(
                    children: loginMap.keys
                        .where((element) => element != "def")
                        .map((e) {
                  return InkWell(
                    onTap: () async {
                      if (loginMap[e]["password"] == "") {
                        route.pushReplacementNamed(MyRoutes.codeLogin,
                            arguments: e);
                      } else {
                        bool res = await MemberService()
                            .login(e, loginMap[e]["password"]);
                        if (res) {
                          MyToast("登录成功");

                          route.pop();
                        }
                      }
                    },
                    child: Column(
                      children: <Widget>[
                        LoginIndex.getHeader(url: loginMap[e]["img"]),
                        SizedBox(height: 20.h),
                        MyText("$e", 32.s, "FF333333", true),
                        SizedBox(height: 10.h),
                        e == loginMap["def"]
                            ? MyText("当前使用", 24.s, '#FF00C657')
                            : Text(""),
                        SizedBox(height: 40.h),
                      ],
                    ),
                  );
                }).toList()
                          ..add(InkWell(
                            onTap: () {
                              route.pushReplacementNamed(MyRoutes.codeLogin);
                            },
                            child: Column(
                              children: <Widget>[
                                Container(
                                    width: 128.h,
                                    height: 128.h,
                                    child: CircleAvatar(
                                        backgroundColor: Colors.white,
                                        child: Image.asset(
                                            "images/user/ic_sign in_add.png"))),
                                SizedBox(height: 20.h),
                                MyText("点击添加", 32.s, "FF333333", true),
                              ],
                            ),
                          ))),
              ],
            ),
            Align(
                alignment: Alignment.bottomCenter,
                child: InkWell(
                  onTap: () {
                    MyStorge.login = {};
                    MyToast("清除成功");
                    route.pushReplacementNamed(MyRoutes.codeLogin);
                  },
                  child: Container(
                      margin: EdgeInsets.only(bottom: 40.h),
                      child: MyText("清除登录记录", 28.s, '#FFFF2B26')),
                ))
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
